//
//  WaitSendViewController.m
//  ZYShang
//
//  Created by wangguangming on 15/7/29.
//  Copyright © 2015年 Amber. All rights reserved.
//

#import "WaitSendViewController.h"
#import "WaitSendview.h"
#import "ReadyDiSignle.h"
#import "Base.h"
#import "WaitsendTableViewCell.h"
#import "Targets.h"
#import "Consignee.h"
#import "DetailViewController.h"
#import "OrderMessagesignle.h"
@interface WaitSendViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    int _type;
    
    double _height;
}
@property(nonatomic,strong)WaitSendview *myview;
@property(nonatomic,strong)UIScrollView *mainScroll;
@end

@implementation WaitSendViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
        {
        self.myview=[[WaitSendview alloc] initWithFrame:[UIScreen mainScreen].bounds];
        }
    return self;
}
-(void)loadView
{
    self.view=self.myview;
    self.myview.mainTable.delegate=self;
    self.myview.mainTable.dataSource=self;
    self.myview.mainTable.separatorStyle = UITableViewCellSelectionStyleNone;

}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setBack];
    _type=1;
    self.title=@"未发货订单";
   
     [[ReadyDiSignle signle] addObserver:self forKeyPath:@"needSendArray" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
 
   [[ReadyDiSignle signle] addObserver:self forKeyPath:@"yudingneedSendArray" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
    
    [self.myview.superButton setTitleColor: BLUECOLOL forState:UIControlStateNormal];
    [self.myview.changeButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.myview.yudingButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    
    self.myview.superLab.backgroundColor=BLUECOLOL;
    self.myview.changeLab.backgroundColor=[UIColor whiteColor];
    self.myview.yudingLab.backgroundColor=[UIColor whiteColor];
    
    
    
    [self.myview.superButton addTarget:self action:@selector(changgeAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.myview.changeButton addTarget:self action:@selector(changgeAction:) forControlEvents:UIControlEventTouchUpInside];
     [self.myview.yudingButton addTarget:self action:@selector(changgeAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
}
-(void)changgeAction:(UIButton *)sender
{
    if ([sender.titleLabel.text isEqualToString:@"超市订单"])
        {
        _type=1;
        [self.myview.superButton setTitleColor: BLUECOLOL forState:UIControlStateNormal];
        [self.myview.changeButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [self.myview.yudingButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        
        self.myview.superLab.backgroundColor=BLUECOLOL;
        self.myview.changeLab.backgroundColor=[UIColor whiteColor];
        self.myview.yudingLab.backgroundColor=[UIColor whiteColor];
       }
    else if([sender.titleLabel.text isEqualToString:@"预定订单"])
        {
        _type=3;
        [self.myview.superButton setTitleColor: [UIColor grayColor] forState:UIControlStateNormal];
        [self.myview.changeButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [self.myview.yudingButton setTitleColor:BLUECOLOL forState:UIControlStateNormal];
        
        self.myview.superLab.backgroundColor=[UIColor whiteColor];
        self.myview.changeLab.backgroundColor=[UIColor whiteColor];
        self.myview.yudingLab.backgroundColor=BLUECOLOL;
        }
    else
        {
        _type=2;
        [self.myview.superButton setTitleColor: [UIColor grayColor] forState:UIControlStateNormal];
        [self.myview.changeButton setTitleColor:BLUECOLOL forState:UIControlStateNormal];
        [self.myview.yudingButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        
        self.myview.superLab.backgroundColor=[UIColor whiteColor];
        self.myview.changeLab.backgroundColor=BLUECOLOL;
        self.myview.yudingLab.backgroundColor=[UIColor whiteColor];
        }
     [self.myview.mainTable reloadData];
}
-(void)observeValueForKeyPath:(nullable NSString *)keyPath ofObject:(nullable id)object change:(nullable NSDictionary *)change context:(nullable void *)context
{
    
    if ([keyPath isEqualToString:@"needSendArray"]) 
        {
        [self.myview.mainTable reloadData];
        
        [[ReadyDiSignle signle] removeObserver:self forKeyPath:@"needSendArray"];
        }
    if ([keyPath isEqualToString:@"yudingneedSendArray"]) 
        {
       
        
        [[ReadyDiSignle signle] removeObserver:self forKeyPath:@"yudingneedSendArray"];
        }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_type==1)
        {
        return [ReadyDiSignle signle].needSendArray.count;
       }
    if(_type==3)
        {
        return [ReadyDiSignle signle].yudingneedSendArray.count;
        }
    else
        
        {
        return 0;
        }
    
}

  
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Targets *target;
    if(_type==1)
        {
    target=[ReadyDiSignle signle].needSendArray[indexPath.row];
        }
    if (_type==3) {
    target=[ReadyDiSignle signle].yudingneedSendArray[indexPath.row];
    }
    Consignee *consignee=[[Consignee alloc] init];
    [consignee setValuesForKeysWithDictionary:target.consignee];
    static NSString *cellid=@"waitcellid";
    WaitsendTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellid];
    if (cell==nil)
        {
        cell=[[WaitsendTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
    }
    cell.backgroundColor=[UIColor whiteColor];
   
    cell.oidLaber.text=[target.oid substringFromIndex:target.oid.length-6];
    cell.oidLaber.textColor=[UIColor redColor];
//    cell.oidLaber.font=[UIFont boldSystemFontOfSize:20];
    NSString *s;
    if (target.howToPay.integerValue==0)
        {
        s=@"货到付款";
      }
    else
        {
        s=@"已经支付";
        }
    
    cell.addressLaber.text=[NSString stringWithFormat:@"地址: %@ %@  %@",consignee.region,consignee.bname,consignee.address];
    cell.addressLaber.lineBreakMode = NSLineBreakByWordWrapping;
    cell.addressLaber.numberOfLines=0;
    CGRect txtFrame = cell.addressLaber.frame;
    
    cell.addressLaber.frame = CGRectMake(10, 100, 300,txtFrame.size.height =[cell.addressLaber.text boundingRectWithSize:
                                                      CGSizeMake(txtFrame.size.width, CGFLOAT_MAX)
                                                                                options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                                                             attributes:[NSDictionary dictionaryWithObjectsAndKeys:cell.addressLaber.font,NSFontAttributeName, nil] context:nil].size.height);
    cell.addressLaber.frame = CGRectMake(5, CGRectGetMaxY(cell.oidLaber.frame), WIDTH-10, txtFrame.size.height);
    
    _height=txtFrame.size.height;
    
    cell.tpLaber.frame=CGRectMake(5, CGRectGetMaxY(cell.addressLaber.frame), WIDTH-10, 25);
    [cell.tpLaber setAdjustsFontSizeToFitWidth:YES];
    cell.moneyLaber.frame=CGRectMake(5, CGRectGetMaxY(cell.tpLaber.frame), WIDTH-10, 15);
    NSLog(@"%lld",consignee.created.longLongValue);
//    NSString *str=[NSString stringWithFormat:@"%lld",consignee.created.longLongValue/1000];//时间戳
//    NSTimeInterval time=[str doubleValue];//因为时差问题要加8小时 == 28800 sec
//    NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
//        // NSLog(@"date:%@",[detaildate description]);
//        //实例化一个NSDateFormatter对象
//    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//        //设定时间格式,这里可以设置成自己需要的格式
//    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//    
//    NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
//    cell.timeLaber.text=[NSString stringWithFormat:@"下单时间:%@",currentDateStr];
    cell.timeLaber.text=[NSString stringWithFormat:@"送货时间段:%@",target.sendDuring];
    cell.tpLaber.text=consignee.cellphone;
    cell.moneyLaber.text=[NSString stringWithFormat:@"¥:%.2lf(%@)",target.totalCast.doubleValue,s];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(CGFloat)tableView:(nonnull UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    return 102+_height;
}
-(void)tableView:(nonnull UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    Targets *target;
    if(_type==1)
    {
    target=[ReadyDiSignle signle].needSendArray[indexPath.row];
    }
    if (_type==3) {
    target=[ReadyDiSignle signle].yudingneedSendArray[indexPath.row];
    }
    [OrderMessagesignle single].oidString=target._id;
    
    [[OrderMessagesignle single] orderMessagworking];
    
    
    DetailViewController *detailViewController=[[DetailViewController alloc] init];
    
    [self.navigationController pushViewController:detailViewController animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
